package edu.mtu.citizenscience.cs.sdk.uploader.model.entry;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import android.content.Context;
import android.location.Address;
import android.location.Geocoder;
import edu.mtu.citizenscience.cs.sdk.uploader.model.Entry;
import edu.mtu.citizenscience.cs.sdk.uploader.model.EntryType;
/**
 * @author Corbin Uselton <corbinu@cwuselto@mtu.edu>
 * @copyright (c) 2012 MichiganTech <http://www.mtu.edu>
 * @since 1.0
 * 
 * Entry of the type location
 */
public class LocationEntry extends Entry<Location> {
	// set the entry type as TYPE_LOCATION
	protected EntryType type = EntryType.TYPE_LOCATION;
	
	// the location city
	private String city;
	// the location state or province
	private String provinceState;
	// the location country
	private String country;
	
	/**
	 * Create Location Entry
	 * 
	 * @param   location - Location - the location for this entry
	 * @return	void
	 */
	public LocationEntry(Location location)
	{
		super("location", EntryType.TYPE_LOCATION, location);
	}
	
	/**
	 * Geolocate the location
	 * 
	 * @param   context - Context - the context to geolocate with
	 * @return	void
	 */
	public void geoLocate(Context context) throws IOException
	{
		// check that have a location value and that the location object stores a location
		if (this.value != null && this.value.haveLocation())
		{
			// get the first address from the location using the geocoder
			Address address = new Geocoder(context).getFromLocation(this.value.getLatitude(), this.value.getLongitude(), 1).get(0);
			// get the city from the address
			this.city = address.getLocality();
			// get the province or state from the address
			this.provinceState = address.getAdminArea();
			// get the country from the address
			this.country = address.getCountryName();
		}
	}
	
	/**
	 * Get the locations city
	 * 
	 * @param   void
	 * @return	String - locations city
	 */
	public String getCity()
	{
		return this.city;
	}
	
	/**
	 * Get the locations province or state
	 * 
	 * @param   void
	 * @return	String - locations province or state
	 */
	public String getProvinceState()
	{
		return this.provinceState;
	}
	
	/**
	 * Get the locations country
	 * 
	 * @param   void
	 * @return	String - locations country
	 */
	public String getCountry()
	{
		return this.country;
	}
	
	/**
	 * Entry type can not be set for the location entry
	 * 
	 * @param   type - FileType - the file type
	 * @return	void
	 */
	public void setType(EntryType type) {  }
	
	/**
	 * Get the location entry as map to upload to the server
	 * 
	 * @param   void
	 * @return	Map<String, Object> - File entry as a map
	 */
	public Map<String, Object> getMap()
	{
		// create a new map to store the location entry
		Map<String, Object> map = new HashMap<String, Object>();
		
		// check if the location entry does not have a value or the value does not have a location
		if ( ! (this.value != null || this.value.haveLocation()))
		{
			// put unknown for both longitude and latitude in the map
			map.put("latitude", "Unknown");
			map.put("longitude", "Unknown");
		}
		else
		{
			// put the latitude and longitude in the map
			map.put("latitude", this.value.getLatitude());
			map.put("longitude", this.value.getLongitude());
		}
		
		// check if location has a city
		if (this.city != null)
		{
			// put the city in the map
			map.put("city", this.city);
		}
		// check if location has a province or state
		if (this.provinceState != null)
		{
			// put the province or state in the map
			map.put("provinceState", this.provinceState);
		}
		// check if location has a country
		if (this.country != null)
		{
			// put the country in the map
			map.put("country", this.country);
		}
		
		// return the location map
		return map;
	}
}
